﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using Zor_Bll;
using Zor_Modes;

namespace Zor_API.Ashx
{
    /// <summary>
    /// UploadFileHandler 的摘要说明
    /// </summary>
    public class UploadFileHandler : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {

            BaseAnswer bar = new BaseAnswer();
            //图片类型
            int picType = int.Parse(context.Request.QueryString["picType"].ToString());

            HttpFileCollection files = context.Request.Files;
            bar = SavePicture(picType, context, bar);
            

            //BaseAnswer bar = new BaseAnswer();
            //DateTime dt = DateTime.Now;
            //CredentialInfo credentialInfo = new CredentialInfo();
            //int id = int.Parse(context.Request.QueryString["Property_Id"].ToString());
            //HttpFileCollection files = context.Request.Files;
            //for (int i = 1; i < 5; i++)
            //{
            //    bar = SavePicture(i, dt, context, bar);
            //    if (bar.AnswerType != BaseAnswer.Answers.Succee)
            //    {
            //        if (i == 1)
            //        {
            //            File.Delete(bar.AnswerMeg.TrimEnd(','));
            //        }
            //        else
            //        {
            //            string[] imageUrl = bar.AnswerMeg.TrimEnd(',').Split(',');
            //            for (int j = 0; j < imageUrl.Length; j++)
            //            {
            //                if (!string.IsNullOrEmpty(imageUrl[j].Trim()))
            //                {
            //                    File.Delete(imageUrl[j]);
            //                }
            //            }
            //        }
            //        bar.AnswerMeg = "图片保存失败!";
            //        i = 5;
            //    }
            //    else 
            //    {
            //        switch (i)
            //        {
            //            case 1:
            //                credentialInfo.Property_Card = bar.AnswerMeg.TrimEnd(',');
            //                break;
            //            case 2:
            //                credentialInfo.Property_IDNumber = bar.AnswerMeg.TrimEnd(',').Split(',')[i - 1];
            //                break;
            //            case 3:
            //                credentialInfo.Property_LeaseContract = bar.AnswerMeg.TrimEnd(',').Split(',')[i - 1];
            //                break;
            //            case 4:
            //                credentialInfo.Property_PaymentCertificate = bar.AnswerMeg.TrimEnd(',').Split(',')[i - 1];
            //                break;
            //            default:
            //                break;
            //        }
            //        if(i==4)
            //        {
            //            credentialInfo.Property_Id = id;
            //            CredentialInfo_Bll.InserCredentialInfo(credentialInfo);
            //        }
            //    }
            //}
        }
        public BaseAnswer SavePicture(int type, HttpContext context, BaseAnswer bar)
        {
            CredentialInfo credentialInfo = new CredentialInfo();
            HttpPostedFile pictureUrl = context.Request.Files.Get("file");
            string savePath = HttpContext.Current.Server.MapPath("~/");
            string filePath = "";
            bar.AnswerType = BaseAnswer.Answers.Failure;
            string strSub = "";
            switch (type)
            {
                case 1:
                    filePath += "Upload\\Card\\";
                    strSub = "fcz";
                    break;
                case 2:
                    filePath += "Upload\\IDCard\\";
                    strSub = "sfz";
                    break;
                case 3:
                    filePath += "Upload\\LeaseContract\\";
                    strSub = "ht";
                    break;
                case 4:
                    filePath += "Upload\\PaymentCertificate\\";
                    strSub = "zm";
                    break;
                default:
                    break;
            }

            if (pictureUrl != null)
            {
                if (!Directory.Exists(savePath + filePath))
                    Directory.CreateDirectory(savePath + filePath);
                string ex = Path.GetFileName(pictureUrl.FileName).Split('.')[1];
                filePath += string.Format("{0}{1}.{2}", strSub, DateTime.Now.ToString("yyyyMMddHHmmss"), ex);
                bar.AnswerMeg = filePath;
                if (!File.Exists(savePath + filePath))
                {
                    credentialInfo.Property_Id = int.Parse(context.Request.QueryString["Property_Id"].ToString());
                    switch (strSub)
                    {
                        case "fcz":
                            credentialInfo.Property_Card = bar.AnswerMeg;
                            break;
                        case "sfz":
                            credentialInfo.Property_IDNumber = bar.AnswerMeg;
                            break;
                        case "ht":
                            credentialInfo.Property_LeaseContract = bar.AnswerMeg;
                            break;
                        case "zm":
                            credentialInfo.Property_PaymentCertificate = bar.AnswerMeg;
                            break;
                        default:
                            break;
                    }
                    bar = CredentialInfo_Bll.addCredentialInfo(credentialInfo);
                    if (bar.AnswerType == BaseAnswer.Answers.Succee)
                    {
                        pictureUrl.SaveAs(savePath + filePath);
                    }
                }
                else
                {
                    bar.AnswerType = BaseAnswer.Answers.Succee;
                }
            }
            return bar;

            //HttpPostedFile pictureUrl = context.Request.Files.Get("cardUrl");
            //string savePath = HttpContext.Current.Server.MapPath("~/") + "Upload/";
            //bar.AnswerType = BaseAnswer.Answers.Failure;
            //switch (type)
            //{
            //    case 1:
            //        pictureUrl = context.Request.Files.Get("cardUrl");
            //        savePath += "/Card/";
            //        break;
            //    case 2:
            //        pictureUrl = context.Request.Files["iDNumberUrl"];
            //        savePath += "/IDCard/";
            //        break;
            //    case 3:
            //        pictureUrl = context.Request.Files["leaseContract"];
            //        savePath += "/LeaseContract/";
            //        break;
            //    case 4:
            //        pictureUrl = context.Request.Files["paymentCertificate"];
            //        savePath += "/PaymentCertificate/";
            //        break;
            //    default:
            //        break;
            //}
            //if (string.IsNullOrEmpty(pictureUrl.FileName))
            //{
            //    if (type == 2 || type == 3)
            //    {
            //        bar.AnswerType = BaseAnswer.Answers.Succee;
            //        bar.AnswerMeg += " ,";
            //    }
            //}
            //else if (!string.IsNullOrEmpty(pictureUrl.FileName))
            //{
            //    savePath += dt.Year + "/" + dt.Month + "/" + dt.Day + "/" + Path.GetFileName(pictureUrl.FileName);
            //    bar.AnswerMeg += savePath + ",";
            //    if (!File.Exists(savePath))
            //    {
            //        bar.AnswerType = BaseAnswer.Answers.Succee;
            //        pictureUrl.SaveAs(savePath);
            //    }
            //    else
            //    {
            //        bar.AnswerType = BaseAnswer.Answers.Succee;
            //    }
            //}
            //return bar;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}